﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GraphicManager.Control
{
    public partial class Loading : UserControl
    {
        List<WaitPoint> ptList = new List<WaitPoint>();
        private class WaitPoint
        {
            public PointF Location;
            public int Times;//跑动时间次数
        }

        private Color pointColor = Color.White;//点的颜色
        private int diameter = 6;//直径
        private int slowSpeed = 1;//慢的速度
        private int fastSpeed = 5;//快速的速度
        private int pointNum = 5;//点的数量
        private float inarea = 0.4f;//慢速区域入点，2/5的位置
        private float outarea = 0.6f;//慢速区域出点，3/5的位置
        private int spaceNum = 5;//初始间隔像素距离
        private int spaceTimes = 10;//初始间隔时间次数
        [Category("参数设置"),Description("圆点的直径")]
        public int Diameter
        {
            get
            {
                return diameter;
            }

            set
            {
                diameter = value;
            }
        }
        [Category("参数设置"), Description("慢速的速度")]
        public int SlowSpeed
        {
            get
            {
                return slowSpeed;
            }

            set
            {
                slowSpeed = value;
            }
        }
        [Category("参数设置"), Description("快速的速度")]
        public int FastSpeed
        {
            get
            {
                return fastSpeed;
            }

            set
            {
                fastSpeed = value;
            }
        }
        [Category("参数设置"), Description("点的数量")]
        public int PointNum
        {
            get
            {
                return pointNum;
            }

            set
            {
                pointNum = value;
            }
        }
        [Category("参数设置"), Description("圆点的颜色")]
        public Color PointColor
        {
            get
            {
                return pointColor;
            }

            set
            {
                pointColor = value;
            }
        }
        [Category("参数设置"), Description("慢速区域进入点")]
        public float Inarea
        {
            get
            {
                return inarea;
            }

            set
            {
                inarea = value;
            }
        }
        [Category("参数设置"), Description("慢速区域出去点")]
        public float Outarea
        {
            get
            {
                return outarea;
            }

            set
            {
                outarea = value;
            }
        }
        [Category("参数设置"), Description("初始间隔像素距离")]
        public int SpaceNum
        {
            get
            {
                return spaceNum;
            }

            set
            {
                spaceNum = value;
            }
        }
        [Category("参数设置"), Description("初始间隔时间次数")]
        public int SpaceTimes
        {
            get
            {
                return spaceTimes;
            }

            set
            {
                spaceTimes = value;
            }
        }

        public Loading()
        {
            InitializeComponent();
        }

        private void loadAnime_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < ptList.Count; i++)
            {
                WaitPoint pt = ptList[i];
           
                if (i > 0)
                {
                    if (ptList[i - 1].Times - ptList[i].Times < spaceTimes)
                    {
                        break;
                    }
                }
                pt.Times++;
                if (pt.Location.X <= this.Width * inarea)
                {
                    pt.Location.X += fastSpeed;
                }
                else if (pt.Location.X > this.Width * inarea && pt.Location.X < this.Width * outarea)
                {
                    pt.Location.X += slowSpeed;
                }
                else
                {
                    pt.Location.X += fastSpeed;

                }
                //最后一个已经出去，再重置
                if (ptList[ptList.Count-1].Location.X >= this.Width)
                {
                    for (int n = 0; n < ptList.Count; n++)
                    {
                        ptList[n].Location = new Point(0 - n * spaceNum, this.Height / 2 - diameter / 2);
                        ptList[n].Times = 0;
                    }
                   
                    break;
                }

                this.Invalidate();
            }

          
        }

        public void StartAnime()
        {
            ptList.Clear();
            for (int i = 0; i < pointNum; i++)
            {
                WaitPoint wp = new WaitPoint();
                wp.Location = new Point(0 - i * spaceNum, this.Height / 2 - diameter / 2);
                ptList.Add(wp);
            }
            this.Invalidate();
            loadAnime.Start();
        }
        public void StopAnime()
        {
            loadAnime.Stop();
        }

        private void Loading_Paint(object sender, PaintEventArgs e)
        {
            BufferedGraphicsContext context = BufferedGraphicsManager.Current;
            BufferedGraphics bg = context.Allocate(e.Graphics, this.ClientRectangle);
            bg.Graphics.Clear(this.BackColor);
            bg.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            for (int i = 0; i < ptList.Count; i++)
            {
                WaitPoint pt = ptList[i];
                bg.Graphics.FillEllipse(new SolidBrush(pointColor), new RectangleF(pt.Location.X, pt.Location.Y, diameter, diameter));
            }
            bg.Render();
            bg.Dispose();
            bg = null;
        }
    }
}
